package com.hancai.web;


import com.github.pagehelper.PageInfo;
import com.hancai.model.TRecordRemark;
import com.hancai.query.RecordRemarkQuery;
import com.hancai.result.R;
import com.hancai.service.RecordRemarkService;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;

/**
 * @author super
 */
@RestController
public class RecordRemarkController {

    @Resource
    private RecordRemarkService recordRemarkService;

    @PostMapping(value = "/api/record/remark")
    public R addRecordRemark(@RequestBody RecordRemarkQuery recordRemarkQuery, @RequestHeader(value = "Authorization") String token) {
        //axios提交post请求，提交过来的是json数据，使用@RequestBody注解接收
        recordRemarkQuery.setToken(token);
        int save = recordRemarkService.saveRecordRemark(recordRemarkQuery);
        return save >= 1 ? R.OK( ) : R.FAIL();
    }

    @GetMapping(value = "/api/record/remark")
    public R recordRemarkPage(@RequestParam(value = "current", required = false) Integer current,
                                @RequestParam(value = "recordId") Integer recordId) {

        RecordRemarkQuery recordRemarkQuery = new RecordRemarkQuery();
        recordRemarkQuery.setRecordId(recordId);

        if (current == null) {
            current = 1;
        }
        PageInfo<TRecordRemark> pageInfo = recordRemarkService.getRecordRemarkByPage(current, recordRemarkQuery);
        return R.OK(pageInfo);
    }


    @GetMapping(value = "/api/record/remark/{id}")
    public R recordRemarkPage(@PathVariable(value = "id") Integer id) {
        TRecordRemark tRecordRemark = recordRemarkService.getRecordRemarkById(id);
        return R.OK(tRecordRemark);
    }

    @PutMapping(value = "/api/record/remark")
    public R editRecordRemark(@RequestBody RecordRemarkQuery recordRemarkQuery, @RequestHeader(value = "Authorization") String token) {
        //axios提交post请求，提交过来的是json数据，使用@RequestBody注解接收
        recordRemarkQuery.setToken(token);
        int update = recordRemarkService.updateRecordRemark(recordRemarkQuery);
        return update >= 1 ? R.OK( ) : R.FAIL();
    }

    @DeleteMapping(value = "/api/record/remark/{id}")
    public R delRecordRemark(@PathVariable(value = "id") Integer id) {
        int del =recordRemarkService.delRecordRemarkById(id);
        return del >= 1 ? R.OK( ) : R.FAIL();
    }
}
